package cn.powertime.evaluation.facade.impl;

import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.annotation.Facade;
import cn.powertime.evaluation.entity.KbKnowledgeBase;
import cn.powertime.evaluation.entity.KbKnowledgeBaseContent;
import cn.powertime.evaluation.facade.KbKnowledgeBaseFacade;
import cn.powertime.evaluation.service.KbKnowledgeBaseContentService;
import cn.powertime.evaluation.service.KbKnowledgeBaseService;
import cn.powertime.evaluation.vo.ParamPageVo;
import cn.powertime.evaluation.vo.ReqKnowledgeBaseSearchVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Facade
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class KbKnowledgeBaseFacadeImpl implements KbKnowledgeBaseFacade {

    @Autowired
    private KbKnowledgeBaseService knowledgeBaseService;
    @Autowired
    private KbKnowledgeBaseContentService knowledgeBaseContentService;

    @Override
    public IPage<KbKnowledgeBase> knowledgeBaseList(ParamPageVo<ReqKnowledgeBaseSearchVo> paramPageVo) {
        QueryWrapper<KbKnowledgeBase> qw = new QueryWrapper<>();
        qw.ne("status", Constants.STATUS_DISABLED);
        qw.ne("status", Constants.STATUS_DEL);
        qw.eq("type", paramPageVo.getParams().getType());
        qw.orderByDesc("id");
        Page<KbKnowledgeBase> page = new Page<>(paramPageVo.getPage().getCurrent(), paramPageVo.getPage().getSize());
        return this.knowledgeBaseService.page(page, qw);
    }

    @Override
    public KbKnowledgeBase details(Long id) {
        return this.knowledgeBaseService.getById(id);
    }

    @Override
    public List<KbKnowledgeBaseContent> content(Long rootId) {
        QueryWrapper<KbKnowledgeBaseContent> qw = new QueryWrapper<>();
        qw.eq("root_id", rootId);
        return this.knowledgeBaseContentService.list(qw);
    }

}
